﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

using DTO;
using DataService;

public partial class Admin_Firewall : BasePage
{
    int customerId = 0;
    int firewallId = 0;
    IList<Device> devices = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        if ( AdminLogin == null )
            Response.Redirect("~/admin/login.aspx");

        Initial();
    }

    private void Initial ()
    {
        if ( !string.IsNullOrEmpty(Request.Params["c"]) )
        {
            int.TryParse(Request.Params["c"], out customerId);            
        }

        if ( customerId == 0 )
            Response.Redirect("~/admin/default.aspx");

        using ( CustomerService service = new CustomerService() )
        {
            litCustomer.Text = service.GetAddressInfoByAccountId(customerId).ToString();
        }

        if ( !string.IsNullOrEmpty(Request.Params["f"]) )
        {
            int.TryParse(Request.Params["f"], out firewallId);
        }

        using ( DeviceService service = new DeviceService() )
        {
            devices = service.GetDevicesDependFirewallByAccountIdAndFirewallId(customerId, firewallId);

            if ( !IsPostBack )
            {
                CalendarFromDate.Format = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
                CalendarToDate.Format = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;

                if ( firewallId > 0 )
                {
                    // get firewall
                    Firewall firewall = service.GetFirewall(firewallId);
                    txtCode.Text = firewall.Code;
                    txtName.Text = firewall.Name;
                    txtIPAddress.Text = firewall.IP;
                    txtFee.Text = firewall.Fee.ToString();
                    txtFromDate.Text = ConvertDateToString(firewall.RentFromDate);
                    txtToDate.Text = ConvertDateToString(firewall.RentToDate);
                }

                grdDevices.DataSource = devices;
                DataBind();
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        double fee = 0;
        double.TryParse(txtFee.Text, out fee);

        DateTime from = ConvertStringToDate(txtFromDate.Text);
        DateTime to = ConvertStringToDate(txtToDate.Text);

        if ( fee == 0 || from == DateTime.MinValue || to == DateTime.MinValue )
            return;

        using ( DeviceService service = new DeviceService() )
        {
            Firewall firewall = service.SaveFirewall(new Firewall(firewallId)
            {
                Code = txtCode.Text,
                Name = txtName.Text,
                IP = txtIPAddress.Text,
                Fee = fee,
                RentFromDate = from,
                RentToDate = to,
                Customer = new Customer(customerId)
            });

            if ( firewall != null )
            {
                IList<Device> removedDevices = new List<Device>();
                IList<Device> boundDevices = new List<Device>();

                int i = 0;
                foreach ( GridViewRow row in grdDevices.Rows )
                {
                    Device device = devices[i];
                    CheckBox checkbox = ( CheckBox ) row.FindControl("chkBindToFirewall");

                    if ( checkbox.Checked )
                    {
                        // bind device to firewall
                        boundDevices.Add(device);
                    }
                    i++;
                }

                service.RemoveDevicesFromFirewall(firewall, devices);
                service.BindDevicesToFirewall(firewall, boundDevices);
            }
        }

        Response.Redirect("~/Admin/Firewalls.aspx?ac=" + customerId.ToString());
    }

    protected void btnCancel_Click ( object sender, EventArgs e )
    {
        Response.Redirect("~/admin/firewalls.aspx?ac=" + customerId);
    }
}